package com.bw.smartpark

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.therouter.TheRouter
import com.therouter.router.Route
import androidx.viewpager2.widget.ViewPager2
import com.bw.announcement.AnnouncementFragment
import com.bw.common.utils.ImmersiveNightModeUtil
import com.bw.home.HomeFragment
import com.bw.message.MessageFragment
import com.bw.mine.MineFragment
import com.bw.services.ServicesFragment
import com.bw.smartpark.adapter.MainBottomAdapter
import com.bw.smartpark.databinding.ActivityMainBinding
import com.google.android.material.bottomnavigation.BottomNavigationView
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "app/MainActivity")
class MainActivity : AppCompatActivity() {
    private lateinit var viewPager: ViewPager2
    private lateinit var bottomNav: BottomNavigationView
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        TheRouter.inject(this)
        
        // 应用保存的主题设置
        ImmersiveNightModeUtil.applySavedNightMode()
        
        // 设置沉浸式状态栏
        ImmersiveNightModeUtil.setGameImmersiveStatusBar(this)
        
        setContentView(R.layout.activity_main)

        viewPager = findViewById(R.id.view_pager)
        bottomNav = findViewById(R.id.bottom_nav)
        bottomNav.itemIconTintList = null
        bottomNav.itemTextColor = ContextCompat.getColorStateList(this, R.color.selector_color)

        setupViewPager()
        setupNavigation()

        // 恢复上次的页面
        val lastPage = savedInstanceState?.getInt("current_page") ?: 0
        viewPager.setCurrentItem(lastPage, false)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        // 保存当前页面
        outState.putInt("current_page", viewPager.currentItem)
    }
    
    override fun onResume() {
        super.onResume()
        // 确保主题设置正确应用
        ImmersiveNightModeUtil.applySavedNightMode()
    }
    
    private fun setupViewPager() {
        // 创建Fragment实例
        val fragments = listOf(
            HomeFragment(),
            ServicesFragment(),
            MessageFragment(),
            AnnouncementFragment(),
            MineFragment()
        )

        // 配置ViewPager2
        viewPager.adapter = MainBottomAdapter(this, fragments)

        // 设置页面缓存（左右各缓存1页 + 当前页 = 3页）
        viewPager.offscreenPageLimit = 2

        // 禁用用户滑动（可选）
        // viewPager.isUserInputEnabled = false
    }

    private fun setupNavigation() {
        // 底部导航点击事件
        bottomNav.setOnItemSelectedListener { item ->
            when(item.itemId) {
                R.id.menu_home -> viewPager.setCurrentItem(0, false)
                R.id.menu_service -> viewPager.setCurrentItem(1, false)
                R.id.menu_message -> viewPager.setCurrentItem(2, false)
                R.id.menu_notice -> viewPager.setCurrentItem(3, false)
                R.id.menu_mine -> viewPager.setCurrentItem(4, false)
            }
            true
        }

        // ViewPager页面切换监听
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                bottomNav.menu.getItem(position).isChecked = true
            }
        })
    }
}